package com.uenpay.oem.ui.trans.transfer;

import android.text.TextUtils;

import com.qiyunxin.android.http.HttpInvokeResultCode;
import com.socks.library.KLog;
import com.uenpay.im.IMApplication;
import com.uenpay.im.ui.old.activity.transfer.TransferHandle;
import com.uenpay.oem.common.AppConstant;
import com.uenpay.oem.data.model.ITransactionModel;
import com.uenpay.oem.data.modelImpl.BankCardModel;
import com.uenpay.oem.data.modelImpl.TransactionModel;
import com.uenpay.service.bean.BankCardInfo;
import com.uenpay.service.bean.TransBankCardList;
import com.uenpay.service.model.IBankCardModel;
import com.uenpay.util.net.listener.HttpOnNextListener;
import com.uenpay.util.net.listener.HttpResultListener;

import java.util.HashMap;

/**
 * Author: liulou
 * Date: 2017/6/23.
 * desc:
 */

public class TransferPresenter implements TransferContract.Presenter {

    private TransferContract.View mView;
    private IBankCardModel mBankCardModel;
    private ITransactionModel mTranscationModel;


    public TransferPresenter(TransferContract.View view) {
        this.mView = view;
        mBankCardModel = new BankCardModel();
        mTranscationModel = new TransactionModel();
    }

    @Override
    public void onStart() {

    }

    @Override
    public void onDestroy() {

    }

    @Override
    public void getTransBankCardList() {
        mBankCardModel.getTransBankList(AppConstant.TradeType.CHARHE, new HttpOnNextListener<TransBankCardList<BankCardInfo>>() {
            @Override
            public void onNext(TransBankCardList<BankCardInfo> bankCardList) {
                mView.setTransBankCardList(bankCardList);
            }
        });
    }

    @Override
    public void sendTransfer(String oprOpenIds, String amount, String payPassword, String payType, String memo) {
        //余额转账走企云信接口，银行卡走自己的
        if ("4".equals(payType)) {
            TransferHandle.transferToPersonal(oprOpenIds, amount, "0.00", payPassword, memo, new TransferHandle.ITransferLisener() {

                @Override
                public void onTransferResult(int status, String msg) {
                    KLog.d("[sendTransfer] status=" + status + " msg=" + msg);
                    if (status == 400 && "密码不正确！".equals(msg)) {
                        mView.payPwdError();
                    } else if (status == HttpInvokeResultCode.RESULT_OK) {
                        mView.transferSuccess();
                    } else if (!TextUtils.isEmpty(msg)) {
                        mView.showToast(msg);
                    }
                }
            });
        } else {
            mTranscationModel.transfer(amount, oprOpenIds, payPassword, payType, memo, new HttpOnNextListener<HashMap<String, String>>() {
                @Override
                public void onNext(HashMap<String, String> stringStringHashMap) {
                    KLog.d("wkPayNo=" + stringStringHashMap.get("wkPayNo") + " tradeNo=" + stringStringHashMap.get("tradeNo") + " payType=" + stringStringHashMap.get("payType"));
                    String wkPayNo = stringStringHashMap.get("wkPayNo");
                    String tradeNo = stringStringHashMap.get("tradeNo");
                    String payType = stringStringHashMap.get("payType");
                    mView.returnTradeInfo(wkPayNo, tradeNo, payType);
                }
            });
        }
    }

    @Override
    public void checkCode(String tradeNo, String wkPayNo, String yzm) {
        mTranscationModel.checkCode(tradeNo, wkPayNo, yzm, new HttpResultListener() {
            @Override
            public void onComplete() {
                super.onComplete();
                mView.transferSuccess();
            }

            @Override
            public void onError(Throwable e) {
                super.onError(e);
                mView.showToast(e.getMessage());
            }
        });
    }
}
